package branch;

import java.util.Random;

public class RandomSnapshotThread implements Runnable {

	private Branch branch;
	private final int MIN_WAIT_TIME = 10000; //10000
	private Random random;

	public RandomSnapshotThread(Branch branch) {
		this.branch = branch;
	}

	@Override
	public void run() {		
		random = new Random();

		int waitTime = 0;
		int randomWaitTime = 0;

		randomWaitTime = random.nextInt(20000);
		waitTime = MIN_WAIT_TIME + randomWaitTime;


		while(true) {
			try {
				Thread.sleep(waitTime);

				System.out.println(String.format("Random Snapshot By: %d",branch.getLocalBranchDAO().getId()));

				branch.initiateSnapShot();

				//Get new waitTime
				randomWaitTime = random.nextInt(20000);
				waitTime = MIN_WAIT_TIME + randomWaitTime;

			} 
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

}
